OUT = libfractal.a  # library name :)

# Dirs with sources
all_source_dirs = . gfractal afractal fractal_compress fractal_compress/domain
# List of src dirs for clean *~ files
clean_src_dirs = $(addprefix ./src/, $(sources_dirs))
# List of src dirs for building (in this case make runs by 
# make_debug(release).sh so we have to use relative path 
# from /bin/debug(release)/
source_dirs = $(addprefix ../../src/, $(sources_dirs))

# List of .cpp files
SRC = $(wildcard $(addsuffix /*.cpp, $(source_dirs)))
# List of .o files without dir names
OBJ = $(notdir $(patsubst %.cpp, %.o, $(SRC)))
DEPS = $(notdir $(patsubst %.cpp, %.P, $(SRC)))
TMPS = $(wildcard $(addsuffix /*~, $(clean_src_dirs)))

VPATH := $(source_dirs)

$(OUT): $(OBJ)
	ar -rc $@ $^
	ranlib $@

%.o : %.cpp
	$(CXX) $(CXXFLAGS) -MD -o $@ $<
	@cp $*.d $*.P; \
	 sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
	 -e '/^$$/ d' -e 's/$$/ :/' < $*.d >> $*.P; \
	rm -f $*.d

clean:
	rm -rf ./bin/* $(TMPS)

-include $(DEPS)